package structure.filter;

import java.util.ArrayList;
import java.util.List;

/**
 * 过滤器模式（Filter Pattern）或标准模式（Criteria Pattern）是一种设计模式，这种模式允许开发人员使用不同的标准来过滤一组对象，
 * 通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式，它结合多个标准来获得单一标准。
 */
public class FilterpatternDemo {
	public static void main(String[] args) {
		List<Person> persons = new ArrayList<Person>();
		persons.add(new Person("Robert", "Male", "Single"));
		persons.add(new Person("John", "Male", "Married"));
		persons.add(new Person("Laura", "Female", "Married"));
		persons.add(new Person("Diana", "Female", "Single"));
		persons.add(new Person("Mike", "Male", "Single"));
		persons.add(new Person("Bobby", "Male", "Single"));
		//选择男性
		Criteria male = new CriteriaMale();
		//选择女性
		Criteria female = new CriteriaFemale();
		//选择单身
		Criteria single = new CriteriaSingle();

		System.out.println("Males: ");
		printPersons(male.meetCriteria(persons));
		System.out.println("\nFemales: ");
		printPersons(female.meetCriteria(persons));

		//过滤出男的 单身
		Criteria singleMale = new AndCriteria(single, male);
		System.out.println("\nSingle Males: ");
		printPersons(singleMale.meetCriteria(persons));
		//过滤出女的单身
		Criteria singleOrFemale = new OrCriteria(single, female);
		System.out.println("\nSingle Or Females: ");
		printPersons(singleOrFemale.meetCriteria(persons));
	}

	public static void printPersons(List<Person> persons) {
		for (Person person : persons) {
			System.out.println("Person : [ Name : " + person.getName() + ", Gender : " + person.getGender()
					+ ", Marital Status : " + person.getMaritalStatus() + " ]");
		}
	}
}
